package cn.zd.erp.auth.role.action;


import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;



import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;








import cn.zd.erp.auth.resource.service.ebi.ResourceEbi;
import cn.zd.erp.auth.resource.vo.Tresource;
import cn.zd.erp.auth.role.service.ebi.RoleEbi;
import cn.zd.erp.auth.role.vo.Trole;
import cn.zd.erp.util.base.BaseAction;
import cn.zd.erp.util.base.Grid;
import cn.zd.erp.util.base.Json;
import cn.zd.erp.util.base.PageResults;
import cn.zd.erp.util.base.Role;
import cn.zd.erp.util.base.Tree;

@Controller
@Scope(value = "prototype")
public class RoleAction extends BaseAction {
	private Trole role;
	private List<Tree> tree;
	private Role list;
	private Json json = new Json();
	private Grid grid;
	private PageResults<Role> pageResults;
	@Autowired
	private RoleEbi roleEbi;
	@Autowired
	private ResourceEbi resourceEbi;
	private String resou;
	
	public Role getList() {
		return list;
	}

	public Json getJson() {
		return json;
	}

	public Grid getGrid() {
		return grid;
	}
	
	//保存所选资源
	public String grant()
	{
		try {
			roleEbi.grant(list);
			json.setSuccess(true);
			json.setMsg("添加成功！");
		} catch (Exception e) {
			json.setMsg("添加失败,请找管理员！");
		}
		return "resultjson";
	}

	//获得自己的权限
	public String get()
	{
		list=roleEbi.getRole(role.getId());
		return "listjson";
	}
	// 返回全部资源树
	public String dataGrid() {
		grid = new Grid();
		pageResults = roleEbi.dataGridCount(role, page, sort, rows, order);
		grid.setRows(pageResults.getResults());
		grid.setTotal((long) pageResults.getTotalCount());
		return "datajson";
	}

	// 返回当前用户的菜单
	public String menutree() {

		return "tree";
	}

	// 所有菜单flag是true 只是菜单 false是包括所有资源
	public String allTree() {
		return "tree";
	}

	// 新增用户
	public String sava() {
		/**
		 * 可能增加一个主菜单会报错
		 */
		Trole trole = roleEbi.getByName(role.getName());
		if (trole != null) {
			json.setMsg("角色名已存在!");
		} else {
			try {
				roleEbi.save(role);
				json.setSuccess(true);
				json.setMsg("添加成功！");
			} catch (Exception e) {
				json.setMsg(e.getMessage());
			}

		}
		return "resultjson";
	}

	// 页面跳转
	// 编辑用户
	public String page() {
		if(role!=null){
			role = roleEbi.get(role.getId());
			Set<Tresource> set = role.getResources();
			boolean b = false;
			resou = "";
			for (Tresource tresource : set) {
				if (b) {
					resou += ",";
				} else {
					b = true;
				}
				resou +=tresource.getId();
			}
		}
		return SUCCESS;
	}

	// 修改保存
	public String edit() {
		System.out.println(resou);
		List<Tresource> rset = new ArrayList<Tresource>();
		if (resou != null) {
			for (String Id : resou.split(",")) {
				rset.add(resourceEbi.get(Long.valueOf(Id)));
			}
		}
		try {
//			通过角色的id获取用户资源
			role.setResources(new HashSet<Tresource>(rset));
			roleEbi.save(role);
			json.setSuccess(true);
			json.setMsg("修改成功！");
		} catch (Exception e) {
			json.setMsg(e.getMessage());
		}
		return "resultjson";
	}

	public String delete() {
		try {
			roleEbi.delete(role);
			json.setSuccess(true);
			json.setMsg("删除成功！");
		} catch (Exception e) {
			json.setMsg(e.getMessage());
		}
		return "resultjson";
	}

	public String tree()
	{
		tree = roleEbi.getTree();
		return "tree";
		
	}

	public Trole getRole() {
		return role;
	}

	public void setRole(Trole role) {
		this.role = role;
	}

	public void setList(Role list) {
		this.list = list;
	}

	public List<Tree> getTree() {
		return tree;
	}

	public String getResou() {
		return resou;
	}

	public void setResou(String resou) {
		this.resou = resou;
	}

	
}
